/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						       placed_event_trigger.cpp

	$Header: /game/placed_event_trigger.cpp $

	$NoKeywords: $

 ************************************************************************/

#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "placed_event_trigger.h"

#include "adventure_map.h"
#include "adv_object_type.h"
#include "army.h"
#include "object_registration.h"
#include "sized_int_types.h"
#include "streambuf_operators.h"

// --------------------------------------------------------------------------
// Unnamed namespace
// --------------------------------------------------------------------------

namespace
{

	int const k_current_version = 1;
	int const k_current_map_version = 1;

	t_object_registration< t_placed_event_trigger >
		g_placed_event_trigger_registration( k_adv_object_event_trigger );
	t_object_registration< t_adv_object_pandoras_box >
		g_pandoras_box_registration( k_adv_object_pandoras_box );

} // Unnamed namespace

// --------------------------------------------------------------------------
// t_placed_event_base members
// --------------------------------------------------------------------------

// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
int t_placed_event_base::get_version() const
{
	return k_current_version;
}

// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
bool t_placed_event_base::is_visible_to( int team_num ) const
{
	return false;
}

// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
bool t_placed_event_base::read( std::streambuf &					stream, 
								t_qualified_adv_object_type const&	type,
								int									version )
{
	return version < 1 || read_string16( stream, m_event_name );
}

// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
bool t_placed_event_base::read_from_map(
	std::streambuf &		stream,
	t_progress_handler *	handler )
{
	int format_version = get< t_uint16 >( stream );
	if ( format_version < 0 || format_version > k_current_map_version )
		return false;

	return format_version < 1 || read_string16( stream, m_event_name );
}

// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
bool t_placed_event_base::write( std::streambuf & stream ) const
{
	return write_string16( stream, m_event_name );
}

// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
bool t_placed_event_base::trigger_event(t_army& army)
{
	if (army.get_owner())
	{
		get_map()->process_placed_events(get_event_name(), army, this);
		return true;
	}
	return false;
}


// --------------------------------------------------------------------------
// t_placed_event_trigger members
// --------------------------------------------------------------------------

// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
bool t_placed_event_trigger::is_triggered_by(t_creature_array const& army) const
{
	return false;
}

// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
bool t_placed_event_trigger::blocks_army( t_creature_array const& army, t_path_search_type path_type ) const
{
	return false;
}

// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
t_screen_rect t_placed_event_trigger::get_shadow_rect() const
{
	return t_screen_rect(0, 0, 0, 0); 
}

// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
t_screen_rect t_placed_event_trigger::get_shadow_rect( t_uint32 current_time ) const
{
	return t_screen_rect(0, 0, 0, 0); 
} 

// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
int	t_placed_event_trigger::get_subimage_count() const
{
	return 0; 
}

// --------------------------------------------------------------------------
// t_adv_object_pandoras_box members
// --------------------------------------------------------------------------

// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
bool t_adv_object_pandoras_box::is_triggered_by(t_creature_array const& army) const
{
	return true;
}

// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
bool t_adv_object_pandoras_box::blocks_army( t_creature_array const& army, t_path_search_type path_type ) const
{
	return t_stationary_adventure_object::blocks_army( army, path_type );
}

// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
void t_adv_object_pandoras_box::activate_trigger( t_army* army, t_adv_map_point const& point,
		                                   t_direction direction, t_adventure_frame* frame )
{
	t_adventure_object_ptr preserver = this;

	trigger_event(*army);
}

// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
bool t_adv_object_pandoras_box::is_visible_to( int team_num ) const
{
	return t_stationary_adventure_object::is_visible_to( team_num );
}
